package com.example.demo.controller;

import com.example.demo.dto.TaskInfoDTO;
import com.example.demo.task.TaskInfo;
import com.example.demo.task.TaskManagerService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.Instant;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;

@RestController
@RequestMapping("/api/tasks")
public class TaskController {

    private final TaskManagerService taskService;

    public TaskController(TaskManagerService taskService) {
        this.taskService = taskService;
    }

    /**
     * 添加任务
     */
    @PostMapping("/add")
    public ResponseEntity<String> addTask(@RequestBody TaskInfoDTO taskInfoDTO) {
        try {
            boolean result = taskService.addTask(taskInfoDTO);
            return ResponseEntity.ok(result ? "任务添加成功" : "任务添加失败");
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }

    /**
     * 暂停任务
     */
    @PostMapping("/pause/{taskId}")
    public ResponseEntity<String> pauseTask(@PathVariable String taskId) {
        try {
            boolean result = taskService.pauseTask(taskId);
            return ResponseEntity.ok(result ? "任务暂停成功" : "任务暂停失败");
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }

    /**
     * 恢复任务
     */
    @PostMapping("/resume/{taskId}")
    public ResponseEntity<String> resumeTask(@PathVariable String taskId) {
        try {
            boolean result = taskService.resumeTask(taskId);
            return ResponseEntity.ok(result ? "任务恢复成功" : "任务恢复失败");
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }

    /**
     * 删除任务
     */
    @DeleteMapping("/delete/{taskId}")
    public ResponseEntity<String> deleteTask(@PathVariable String taskId) {
        try {
            boolean result = taskService.deleteTask(taskId);
            return ResponseEntity.ok(result ? "任务删除成功" : "任务删除失败");
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }

    /**
     * 查询所有任务
     */
    @GetMapping("/list")
    public ResponseEntity<List<TaskInfo>> getAllTasks() {
        return ResponseEntity.ok(taskService.getAllTasks());
    }

    /**
     * 查询单个任务
     */
    @GetMapping("/{taskId}")
    public ResponseEntity<TaskInfo> getTaskById(@PathVariable String taskId) {
        return ResponseEntity.ok(taskService.getTaskById(taskId));
    }

    /**
     * 运行一次
     * @param id id
     */
    @PostMapping("/{id}/runOnce")
    public void runOnce(@PathVariable String id) {
        taskService.runOnce(id);
    }
}